<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final
 * @category    Controllers
 * @see 		Dashboard
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Dashboard extends CI_Controller {
	var $uri_arr = array();

	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		$this->load->model('dashboard_model', 'Das', true);
		$this->uri_arr = array(
			'rowstart' => '',
			'deleted' => ''
		);
	}

	public function index() {
		global $modules_groups;
		$methods =& get_instance();
		/*
		* Xu ly du lieu
		*/
		if ($this->session->userdata('logged_in') == true)
		{
			$data = array(
				'dashboard' => array(),
				'modules_groups' => $modules_groups,
				'methods' => $methods
			);
			for ($i = 0; $i < count($modules_groups); $i++)
			{
				$rows = $this->Das->show($i);
				$data['tab_dashboard'][$i] = '';
				if ($rows) $data['tab_dashboard'][$i] = $rows;
			}
			/*
			* Hien thi
			*/
			$header = array(
				'title' => 'Bảng điều khiển',
				'add_link' => '',
				'search' => false,
				'page_list' => false,
				'datetime_picker' => false,
				'submit_btn' => false,
				'cat_list' => array(),
				'uri' => $this->uri_arr,
				'act' => $GLOBALS['var']['act'],
				'do' => $GLOBALS['var']['do'],
				'id' => $GLOBALS['var']['id'],
				'filter_cat' => $GLOBALS['var']['filter_cat'],
				'deleted' => $GLOBALS['var']['deleted']
			);
			$this->load->view('header', $header);
			$this->load->view('dashboard', $data);
			$this->load->view('footer');
		}
		else
		{
			$ip = $this->input->ip_address();
			$data = array(
				'error' => '',
				'blacklist' => array(),
				'failed' => $this->input->get('failed', true)
			);
			$data['blacklist']['failed'] = get_data('blacklists', "ip = '$ip'", 'failed');
			$data['blacklist']['time'] = get_data('blacklists', "ip = '$ip'", 'time');
			$data['blacklist']['remaining'] = $data['blacklist']['time'] - time();
			$data['blacklist']['active'] = get_data('blacklists', "ip = '$ip'", 'active');
			/*
			* Kiem tra POST method
			*/
			if (!empty($_POST['username']))
			{
				if ($data['blacklist']['remaining'] > 0)
				{
					redirect();
				}
				$username = $this->input->post('username', true);
				$password = $this->input->post('password', true);
				if ($username == '') $data['error'] = 'Vui lòng nhập username!';
				else if ($password == '') $data['error'] = 'Vui lòng nhập password!';
				else
				{
					$this->load->library('form_validation');
					$this->form_validation->set_rules('username', 'Username', 'trim|xss_clean');
					$this->form_validation->set_rules('password', 'Password', 'trim|xss_clean');
					if ($this->form_validation->run() == true) {
						if ($this->Das->login($username, $password))
						{
							redirect();
						}
						else
						{
							$this->Das->add_blacklist($ip);
							redirect('?failed=1');
						}
					}
				}
			}
			/*
			* Hien thi
			*/
			$this->load->view('login', $data);
		}
	}

	public function logout()
	{
		write_log('logout');
		$session = array(
			'user_id' => '',
			'token' => md5(uniqid().microtime().rand()),
			'logged_in' => FALSE
		);
		$this->session->unset_userdata($session);
		setcookie('logged_in', 0, 0, '/');
        setcookie('user_id', 0, 0, '/');
		redirect();
	}

	public function get_new($module)
	{
		return $this->Das->get_new($module);
	}

	public function reset_password()
	{
		if (!$this->input->is_ajax_request())
		{
		   exit('No direct script access allowed');
		}
		$step = $this->input->post('step', true);
		$code = $this->input->post('code', true);
		if ($step == 1)
		{
			$user = $this->input->post('user', true);
			$id = get_data('users', 'username = "'.$user.'"', 'id');
			if ($id > 0)
			{
				$email = get_data('users', 'id = "'.$id.'"', 'email');
				if ($email != '')
				{
					$code = $this->Das->reset_password($id, $email);
					$content = $this->load->view('users/reset_password', array('code' => $code), true);
					$config = array(
						'protocol' => 'smtp',
				        'smtp_host' => 'mail.hitigroup.com',
				        'smtp_port' => '25',
				        'smtp_user' => 'resetpassword@hitigroup.com',
				        'smtp_pass' => '@123456',
				        'mailtype'  => 'html',
				        'charset' => 'utf-8',
				        'wordwrap' => TRUE
				    );
				    $this->load->library('email', $config);
				    $this->email->from($config['smtp_user'], 'HITI AdminCP Password');
				    $this->email->to($email);
				    $this->email->subject('Ma xac nhan yeu cau thay doi mat khau HITI AdminCP');
				    $this->email->message($content);
				    if ($this->email->send())
				    {
				    	echo 1;
				    }
				    else
				    {
				    	echo 3;
				    }
				}
				else
				{
					echo 2;
				}
			}
			else
			{
				echo 0;
			}
		}
		else if ($step == 2)
		{
			if ($code)
			{
				$id = get_data('users', 'password = "'.$code.'"', 'id');
				if ($id > 0)
				{
					echo 1;
				}
				else
				{
					echo 2;
				}
			}
			else
			{
				echo 0;
			}
		}
		else if ($step == 3)
		{
			$pass = $this->input->post('pass', true);
			if ($code && $pass)
			{
				$id = get_data('users', 'password = "'.$code.'"', 'id');
				if ($id > 0)
				{
					$session = array(
		                'logged_in' => true,
		                'token' => md5(uniqid().microtime().rand().$id),
		                'user_id' => $id
		            );
		            $this->session->set_userdata($session);
		            setcookie('logged_in', 1, time() + COOKIE_TIME, '/');
		            setcookie('user_id', $id, time() + COOKIE_TIME, '/');
		            write_log('login');
	                $update = array(
	                	'password' => md5($pass),
	                	'last_login' => date(TIME_SQL),
	                	'ip' => $this->input->ip_address()
	                );
	                $this->db->where('id', $id);
	                $this->db->update('users', $update);
					echo 1;
				}
				else
				{
					echo 2;
				}
			}
			else
			{
				echo 0;
			}
		}
	}

}

/* End of file dashboard.php */
/* Location: ./application/controllers/dashboard.php */